<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width='550px' append-to-body>
      <el-form   label-width="140px">
              <el-form-item  label='项目名称：'>
                    
              </el-form-item>
              <el-form-item  label='产品名称：'>
                    {{rowform.name}}
              </el-form-item>
              <el-form-item  label='产品编号：'>
                    
              </el-form-item>
              <el-form-item  label='标的：'>
                    <el-button type="primary" icon="el-icon-plus" size="small" round @click="add()"  class='mb16'>新增</el-button>
              </el-form-item>
              <el-form-item  
                              v-for="(domain, index) in biaodi"
                              :label="'标的' + (index+1) + '：'"
                              :key="index"
                              >

                              <el-input v-model.trim="domain.name" placeholder='请输入'  class='w260'></el-input>
                              <span  v-if='biaodi.length!="1"'><i class="el-icon-circle-close ft18 color-red cursor" type='' @click.prevent="removeDomain(index)"></i></span>

                </el-form-item>
      </el-form>
       <div slot="footer" class="dialog-footer">
              <el-button @click="dialogProps.visible = false">取 消</el-button>
              <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
  </el-dialog>
</template>

<script charset="utf-8" type="text/javascript">
  import  { saveAddBiao } from '@/api/professional_work/product/index'
export default {
  name: 'ManageBiao',
  data() {

    return {
      loading: false,
      //dialogo基本信息  显示隐藏状态  title 
      dialogProps: {
        visible: false,
        action: '',
        title: '管理标的'
      },
      
      biaodi:[{name:''}],
      
      
      //row
      rowform:{

      },
      rowcode:'',

    }
  },
  created(){
    
    
  },
  methods: {
   
    submit(formName) {
      this.loading = true
      let that = this
/*waiting**/
      let searchPromises = []
      for (let element of this.biaodi) {
          // for (const value of element.behaviorDetails) {

            // if no longitude nor latitude, get them from adminAreaCode
            const  name  = element.name;
            
            if (name) {
              // const districtSearch = new IMAP.DistrictSearch();

              const searchPromise = new Promise((resolve, reject) => {
                //每一个promise
                // console.log(name)
                saveAddBiao(that.rowcode,name).then(res=>{
                  // this.loading=false
                  if(res.data.flag == true){
                      that.$message({type:'success',message:res.data.msg})
                      that.dialogProps.visible=false
                      resolve(true);
                  }
                  else{
                    reject();
                  }
                  
                }).catch(error=>{
                  console.log(error)
                })
              });

              searchPromises.push(searchPromise);
            } else  {
              console.log(name)
            }
          // }
        }

          Promise.all(searchPromises).then(values => { 
              console.log(values);
            }, reason => {
              console.log(reason)
            });

        // const results = await Promise.all(searchPromises);
        



      
    },
    //添加标的
    add() {
        //设置最后一条如果没有任何数据，不再添加
        let arr = this.biaodi
        let index = arr.length-1
        // console.log(index)
        if(index>=0){
            // console.log(arr[index].name)
            if(arr[index].name == ''){
              return 
          }
        }
        arr.push({
          name: '',  
          
        });
    },
    removeDomain(index){
      this.biaodi.splice(index,1)
    }
    
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openDialog', function(row) {
        this.dialogProps.visible = true
        this.rowform = row
        this.rowcode = row.code
        // console.log(row)
      })
      
    })
  },
  updated(){
    
  },
  components:{
    
  }
}
</script>

<style  scoped>
</style>


